package small.common.core.thread.util;

/**
 * @Author zhoujin
 * @Date 2022/11/22 17:14
 */
public class ThreadUtil {

    public static final int DEFAULT_THREAD_COUNT =
            Runtime.getRuntime().availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2 : 1;


    /**
     * 通过核心数，计算适当的线程数。
     *
     * @param threadMultiple  线程数
     * @return thread count
     */
    public static int getSuitableThreadCount(int threadMultiple) {
        final int coreCount = Runtime.getRuntime().availableProcessors();
        int workerCount = 1;
        while (workerCount < coreCount * threadMultiple) {
            workerCount <<= 1;
        }
        return workerCount;
    }
}
